package com.zy.edu.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.edu.bean.*;
import com.zy.edu.config.SystemParam;
import com.zy.edu.dao.CourseChapterDocDao;
import com.zy.edu.service.CourseChapterDocService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zy.edu.service.CourseChapterService;
import com.zy.edu.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HuangHaiShui
 * @since 2021-07-24
 */
@Service
public class CourseChapterDocServiceImpl extends ServiceImpl<CourseChapterDocDao, CourseChapterDoc> implements CourseChapterDocService {

    @Resource
    UserService userService;
    @Resource
    CourseChapterService courseChapterService;
    @Override
    public Map saveOrUpdate(CourseChapterDoc courseChapterDoc, InputParam inputParam) {


        if(StringUtils.isEmpty(courseChapterDoc.getChapterId())){
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }

        //当前用户的信息
        User currentUser = userService.selectById(inputParam.getUserId());
        //只有超管和校管可以上传课时
        if(currentUser.getRoleType().equals(Role.SUPER)
                ||currentUser.getRoleType().equals(Role.SCHOOL)) {
            if (StringUtils.isEmpty(courseChapterDoc.getId())) {

                CourseChapter courseChapter = courseChapterService.selectById(courseChapterDoc.getChapterId());
                if(courseChapter == null){
                    return ResponseInfo.error(SystemParam.ERROR_PARAMS);
                }
                courseChapterDoc.setCourseId(courseChapter.getCourseId());

                courseChapterDoc.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                courseChapterDoc.setCreateTime(new Date());
                courseChapterDoc.setStatus(1);
                this.baseMapper.insert(courseChapterDoc);
            } else {
                this.baseMapper.updateById(courseChapterDoc);
            }
            return ResponseInfo.success(null);
        }else {
            return ResponseInfo.error("您无权限创建课时");
        }
    }

    @Override
    public Map list(CourseChapterDoc courseChapterDoc, InputParam inputParam) {
        if(StringUtils.isEmpty(courseChapterDoc.getChapterId())){
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }
        EntityWrapper<CourseChapterDoc> entityWrapper = new EntityWrapper<CourseChapterDoc>();
        if(!StringUtils.isEmpty(courseChapterDoc.getName())){
            entityWrapper.like("name",courseChapterDoc.getName());
        }
        if(!StringUtils.isEmpty(courseChapterDoc.getTypeStr())){
            entityWrapper.eq("type_str",courseChapterDoc.getTypeStr());
        }
        entityWrapper.eq("chapter_id",courseChapterDoc.getChapterId());
        entityWrapper.eq("status",1);
        entityWrapper.orderBy("num",true);
        List<CourseChapterDoc> chapterList = this.baseMapper.selectList(entityWrapper);
        return ResponseInfo.success(chapterList);
    }

    @Override
    public Map dele(CourseChapterDoc chapter) {
        if(!StringUtils.isEmpty(chapter.getId())){
            //设置为不可用
            chapter.setStatus(0);
            this.baseMapper.updateById(chapter);
        }
        return ResponseInfo.success(null);
    }

    @Override
    public Map getById(CourseChapterDoc courseChapterDoc, InputParam inputParam) {
        if(StringUtils.isEmpty(courseChapterDoc.getId())) {
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }else{
            CourseChapterDoc temp = this.selectById(courseChapterDoc.getId());
            return ResponseInfo.success(temp);
        }

    }
}
